import { readFile, writeFile } from '@/api/sys'
import request from '@/utils/request'
import qs from 'qs' // qs库应该是axios安装时会自带的
import { ipcRenderer } from 'electron'
import { getObjByString, parseTime } from '@/utils'
let fs = require('fs')
const path = require('path')

// 获得公司信息
export function get_companyinfo(companyId) {
  let path = '.\\localDataBase\\' + companyId + '\\companyInfo.json'
  return new Promise((resolve, reject) => {
    readFile(path).then((res) => {
      resolve(res);
    })
      .catch((err) => {
        reject(err)
      })
  })
}

// 获得待审核对象(无下一步审核对象时，返回false)
function getNextApprover(relationship, currentId) {
  let people
  relationship.forEach((item, index) => {
    if (item.id === currentId) {
      if (relationship.length == index + 1) {
        return
      }
      people = relationship[index + 1]
    }
  })
  return people || false
}

// 添加|修改请假数据
function do_create_fakeinfo_one(data, newdata) {
  data = data || {}
  newdata = newdata || {}
  const { uid, relationship } = newdata
  Object.assign(data, newdata)
  data['createTime'] = parseTime(new Date()) // 最后一次修改的时间
  data['Approvers'] = {}
  data['state'] = 0 // 待审核状态
  const next = getNextApprover(relationship, uid)
  // 通知下一个
  if (next) {
    data.Approvers[next.id] = { status: 0, reason: '' }
    console.log('通知下一个')
  }
  return data
}
export function create_fakeinfo_one(obj) {
  const { companyId } = obj
  const path = '.\\localDataBase\\' + companyId + '\\list.json'
  return new Promise((resolve, reject) => {
    readFile(path).then((res) => {
      res = getObjByString(res)
      let { id } = obj
      id = id && id in res ? id : Object.keys(res).length + 1
      obj['id'] = id
      res[id] = do_create_fakeinfo_one(res[id], obj)
      writeFile(path, JSON.stringify(res)).then((res) => {
        resolve({ res: 0, msg: "success", data: { id: id } })
      }).catch((err) => {
        reject(err)
      })
    }).catch((err) => {
      reject(err)
    })
  })
}

// 获得员工某条请假数据
export function get_fakeinfo_one(obj) {
  const { companyId, id } = obj
  const path = '.\\localDataBase\\' + companyId + '\\list.json'
  return new Promise((resolve, reject) => {
    readFile(path).then((res) => {
      res = getObjByString(res)
      resolve(res[id])
    }).catch((err) => {
      reject(err)
    })
  })
}

// 审核请假数据 
function do_verify_fakeinfo_one(data, newdata) {
  data = data || {}
  newdata = newdata || {}
  const { relationship, companyId } = data
  const { uid, status, reason } = newdata
  // 修改审核者状态
  data.Approvers[uid] = { status: status, reason: reason }
  // let res = await get_companyinfo(companyId);
  // console.log("这里",res)
  // let url = this.companyInfo.cbUrl
  // let data = {
  //   auditor: this.getFId(),
  //   fakeImg: this.form,
  //   msg: '请审核此申请单'
  // }
  // send_notice(url, data).then(() => {
  //   alert('已成功通知审核员')
  // })
  if (status === 1) {
    const next = getNextApprover(relationship, uid)
    // 通知下一个
    if (next) {
      data.Approvers[next.id] = { status: 0, reason: '' }
      console.log('通知下一个')
    }
    // 通知申请者
    else {
      data.state = 1 // 审核成功
      console.log('通知申请者')
    }
  }
  if (status === 2) {
    // 通知申请者
    data.state = 2 // 审核失败
    console.log('通知申请者')
  }
  return data
}
export function verify_fakeinfo_one(obj) {
  const { companyId, id } = obj
  const path = '.\\localDataBase\\' + companyId + '\\list.json'
  return new Promise((resolve, reject) => {
    readFile(path).then((res) => {
      res = getObjByString(res)
      res[id] = do_verify_fakeinfo_one(res[id], obj)
      writeFile(path, JSON.stringify(res)).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    }).catch((err) => {
      reject(err)
    })
  })
}

// 获得员工的所有列表(申请和审核)
function do_get_fakelist(data, uid) {
  let initiatorList = []
  let ApproverList = []
  for (let k in data) {
    let item = data[k]
    if (item.uid == uid) {
      initiatorList.push(item)
    }
    if (uid in item.Approvers) {
      ApproverList.push(item)
    }
  }
  return { initiatorList, ApproverList }
}
export function get_fakelist(obj) {
  const { companyId, uid } = obj
  const path = '.\\localDataBase\\' + companyId + '\\list.json'
  return new Promise((resolve, reject) => {
    readFile(path).then((res) => {
      res = getObjByString(res)
      resolve(do_get_fakelist(res, uid))
    }).catch((err) => {
      reject(err)
    })
  })
}
